//
//  ShareScrollView.m
//  BaseAPP
//
//  Created by Evisu on 17/7/21.
//  Copyright © 2017年 evisum. All rights reserved.
//

#import "ShareScrollView.h"

//controlPage 高度
#define KCONTROLPAGE_HEIGHT 28

//点击图标的高度
#define KITEM_HEIGHT 60

@interface ShareScrollView ()<UIScrollViewDelegate>

//顶部栏分割线
@property (nonatomic, strong)UIView *ivTopLine;

@property (nonatomic, strong)UIScrollView *scrollView;

@property (nonatomic, strong)UIPageControl *pageControl;

@end

@implementation ShareScrollView

- (instancetype)init {

    if (self = [super init]) {
        
        [self initViews];
    }
    return self;
}


- (void)initViews {

    self.backgroundColor = HEXColor(@"F4F4F6");

    self.ivTopLine = [[UIView alloc] init];
    self.ivTopLine.backgroundColor = HEXColor(@"D7D7D8");
    
    [self addSubview:self.ivTopLine];
}


- (void)layoutViews {
    
    self.ivTopLine.sd_layout
    .topSpaceToView(self, 1)
    .leftEqualToView(self)
    .rightEqualToView(self)
    .heightIs(0.5);
    
    //UIScrollView
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    CGFloat totalPage = ceilf((CGFloat)self.shareModel.count/8);//总页数
    DMLog(@"%f", totalPage);
    CGFloat ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    self.scrollView.contentSize = CGSizeMake(ScreenWidth*totalPage, self.height_sd);
    
    //pageControl
    self.pageControl = [[UIPageControl alloc] init];
    self.pageControl.numberOfPages = totalPage;
    self.pageControl.currentPage = 0;
    self.pageControl.currentPageIndicatorTintColor = HEXColor(@"8B8B8B");
    self.pageControl.pageIndicatorTintColor = HEXColor(@"BBBBBB");
    [self.pageControl addTarget:self action:@selector(didClickedPageControl) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];

    self.pageControl.sd_layout
    .centerXEqualToView(self)
    .widthRatioToView(self, 1)
    .heightIs(KCONTROLPAGE_HEIGHT)
    .bottomSpaceToView(self, 0);
    
    self.scrollView.sd_layout
    .leftEqualToView(self)
    .rightEqualToView(self)
    .topEqualToView(self)
    .bottomSpaceToView(self, KCONTROLPAGE_HEIGHT);
    
    
    CGFloat marginTop = 14;
    CGFloat marginLeft = 36;
    CGFloat margin = ([UIScreen mainScreen].bounds.size.width-2*marginLeft - 4*KITEM_HEIGHT)/3;
    CGFloat row = 0;
    CGFloat col = 0;
    CGFloat X = 0;
    CGFloat Y = 0;
    CGFloat width = KITEM_HEIGHT;
    CGFloat height = KITEM_HEIGHT;
    
    
    for (int i = 0; i < self.shareModel.count; i++) {
        
        int page = i/8;
        
        row = i/4%2;
        col = i% 4;
        X = marginLeft+(margin+width)*col + page*[UIScreen mainScreen].bounds.size.width;
        Y = marginTop+(height+36)*row;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i+100;
        btn.backgroundColor = HEXColor(@"FBFBFB");
        [btn.layer setMasksToBounds:YES];
        [btn.layer setCornerRadius:10.0]; //设置矩圆角半径
        [btn.layer setBorderWidth:0.5];   //边框宽度
        CGColorSpaceRef colorSpace1 = CGColorSpaceCreateDeviceRGB();
        CGColorRef colorref1 = CGColorCreate(colorSpace1,(CGFloat[]){ 218/255.0, 218/255.0, 219/255.0, 1 });
        [btn.layer setBorderColor:colorref1];//边框颜色
        CGColorRelease(colorref1);
        CGColorSpaceRelease(colorSpace1);
        [btn addTarget:self action:@selector(didClickedDownShareButton:) forControlEvents:UIControlEventTouchDown];
        [btn addTarget:self action:@selector(didClickedUpShareButton:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *lab = [[UILabel alloc] init];
        lab.font = [UIFont systemFontOfSize:12.0];
        lab.textColor = HEXColor(@"888888");
        
        [self.scrollView addSubview:btn];
        [self.scrollView addSubview:lab];
        
        btn.frame = CGRectMake(X, Y, width, height);
        
        [lab setSingleLineAutoResizeWithMaxWidth:KITEM_HEIGHT];
        
        lab.sd_layout
        .centerXEqualToView(btn)
        .topSpaceToView(btn, 6)
        .autoHeightRatio(0);
        
        //赋值
        NSArray *items = self.shareModel[i];
        [btn setBackgroundImage:[UIImage imageNamed:items.firstObject] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:items.firstObject] forState:UIControlStateHighlighted];
        lab.text = items.lastObject;
        
    }
}

- (void)setShareModel:(NSArray *)shareModel {

    _shareModel = shareModel;
    
    [self layoutViews];

}

#pragma mark 根据页面的切换改变pageControl
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offSetX = scrollView.contentOffset.x;
    CGFloat pageWidth = self.width_sd;
    
    self.pageControl.currentPage = offSetX/pageWidth+0.5;
}

#pragma mark 点击pageControl切换页面
- (void)didClickedPageControl {

    [UIView animateWithDuration:0.25 animations:^{
        
        self.scrollView.contentOffset = CGPointMake(self.width_sd*self.pageControl.currentPage, 0);
    }];
}

- (void)didClickedDownShareButton:(UIButton *)btn {

    btn.backgroundColor = HEXColor(@"DBDBDD");
}

#pragma mark 点击选项
- (void)didClickedUpShareButton:(UIButton *)btn {
    
    btn.backgroundColor = HEXColor(@"FBFBFB");
    
    NSMutableDictionary *dic= [NSMutableDictionary dictionary];
    switch (btn.tag-100) {
        case 0:
            [dic setValue:@(ShareMore_pic) forKey:KSHARE_MORE];
            break;
            
        case 1:
            [dic setValue:@(ShareMore_video) forKey:KSHARE_MORE];
            break;
            
        case 2:
            [dic setValue:@(ShareMore_videovoip) forKey:KSHARE_MORE];
            break;
            
        case 3:
            [dic setValue:@(ShareMore_location) forKey:KSHARE_MORE];
            break;
            
        case 4:
            [dic setValue:@(ShareMore_redbag) forKey:KSHARE_MORE];
            break;
            
        case 5:
            [dic setValue:@(ShareMore_pay) forKey:KSHARE_MORE];
            break;
            
        case 6:
            [dic setValue:@(ShareMore_friendcard) forKey:KSHARE_MORE];
            break;
            
        case 7:
            [dic setValue:@(ShareMore_myfav) forKey:KSHARE_MORE];
            break;
            
        case 8:
            [dic setValue:@(ShareMore_wallet) forKey:KSHARE_MORE];
            break;
            
        default:
            break;
    }
    
    [BaseNotificationCenter postNotificationName:KSHARE_MORE object:nil userInfo:dic];
}

@end
